package org.mch.func.file.bijiao;

import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;

import org.apache.commons.io.FileUtils;
import org.mch.biz.ui.BizEvent;
import org.mch.biz.ui.BizPanel;

/**
 * 描述：
 * 
 * @author MCHUN；
 * 
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ClientEvent extends BizEvent {
	private static final long serialVersionUID = 3966324554294328392L;

	JFileChooser fileChooser;

	public ClientEvent(BizPanel panel) {
		super(panel);
	}

	protected void buttonClicked(MouseEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_folder()) {
			browse();
		}
		if (e.getSource() == getPanel().getBtn_execute()) {
			execute();
		}
		if (e.getSource() == getPanel().getBtn_delfile()) {
			delSelFile();
		}
	}

	public void delSelFile() throws Exception {
		List<?> sels = getPanel().getList_file().getSelectedValuesList();

		if (sels != null && sels.size() > 0) {
			for (int i = 0, len = sels.size(); i < len; i++) {
				File file = (File) sels.get(i);
				if (file.isFile()) {
					file.delete();
				} else {
					FileUtils.deleteDirectory(file);
				}
			}

			execute();
		}
	}

	public void execute() {
		File file = new File(getPanel().getText_folder().getText());
		File[] files = file.listFiles();
		if (files == null || files.length <= 0) {
			return;
		}

		String FuHao = getPanel().getText_sepsplit().getText();
		String text_sepnumber = getPanel().getText_sepnumber().getText();
		int position = 0;
		if (text_sepnumber == null || text_sepnumber.trim().equals("")) {
			position = 1;
		} else {
			position = Integer.parseInt(text_sepnumber);
		}

		Map<String, List<File>> filemap = new HashMap<String, List<File>>();
		for (int i = 0, len = files.length; i < len; i++) {
			File tmp = files[i];
			String name = tmp.getName();
			String sepname = name.substring(0, getFuHaoIndex(name, FuHao, position, 0));
			if (tmp.isDirectory()) {
				sepname = "D" + sepname;
			}

			if (filemap.containsKey(sepname)) {
				filemap.get(sepname).add(tmp);
			} else {
				List<File> list = new ArrayList<File>();
				list.add(tmp);
				filemap.put(sepname, list);
			}
		}

		DefaultListModel model = new DefaultListModel();

		String[] keys = filemap.keySet().toArray(new String[0]);
		for (int i = 0, len = keys.length; i < len; i++) {
			String key = keys[i];
			List<File> fl = filemap.get(key);
			if (fl != null && fl.size() > 1) {
				if (model.getSize() > 0) {
					model.addElement(new File(" "));
				}
				for (int f = 0, flen = fl.size(); f < flen; f++) {
					model.addElement(fl.get(f));
				}
			}
		}

		getPanel().getList_file().setModel(model);
	}

	protected int getFuHaoIndex(String str, String fh, int position, int fromIndex) {
		if (position < 1 || str.length() < fromIndex) {
			return fromIndex == 0 ? 0 : fromIndex - 1;
		} else {
			int index = str.indexOf(fh, fromIndex);
			if (index == -1) {
				return fromIndex == 0 ? 0 : fromIndex - 1;
			}
			return getFuHaoIndex(str, fh, position - 1, index + 1);
		}
	}

	public void browse() {
		int tmp = getFileChooser().showOpenDialog(this.getPanel());
		if (tmp == JFileChooser.APPROVE_OPTION) {
			getPanel().getText_folder().setText(getFileChooser().getSelectedFile().getPath());
		}
	}

	public JFileChooser getFileChooser() {
		if (fileChooser == null) {
			fileChooser = new JFileChooser();
			fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);// ����ѡ��Ŀ¼���ļ�
		}
		return fileChooser;
	}

	public ClientPanel getPanel() {
		return (ClientPanel) super.getPanel();
	}
}